<template>
    <article 
        class="company-card" 
        @click="handleClick" 
        @keydown.enter="handleClick"
        @keydown.space.prevent="handleClick"
        ref="cardRef"
        tabindex="0"
        role="button"
        :aria-label="`查看${company.name}的详细信息`"
    >

        <!-- 企业Logo -->
        <div class="company-card__logo" role="img" :aria-label="`${company.name}企业标志`">
            <!-- 加载中状态 -->
            <div v-if="imageLoading && !imageError" class="company-card__logo-loading">
                <div class="loading-spinner"></div>
            </div>
            
            <!-- 图片加载成功 -->
            <img 
                v-show="!imageLoading && !imageError && isValidImageUrl(company.logo)" 
                :data-src="company.logo" 
                :alt="`${company.name} logo`"
                :class="{ 'lazy-loaded': imageLoaded }"
                @load="handleImageLoad"
                @error="handleImageError"
                ref="imageRef"
            />
            
            <!-- 图片加载失败或无效URL时显示占位符 -->
            <div v-if="imageError || !isValidImageUrl(company.logo)" class="company-card__logo-placeholder">
                {{ getInitial(company.name) }}
            </div>
        </div>

        <!-- 企业信息 -->
        <div class="company-card__content">
            <h3 class="company-card__name">{{ company.name }}</h3>
            <p class="company-card__description" v-if="company.description && company.description !== '暂无简介'">{{ company.description }}</p>
        </div>
    </article>
</template>

<script setup>
import { ref, onMounted, onUnmounted } from 'vue';

const props = defineProps({
    company: {
        type: Object,
        required: true,
        validator: (value) => {
            return value.id && value.name && value.industry && value.description;
        }
    }
});

const emit = defineEmits(['click']);

const cardRef = ref(null);
const imageRef = ref(null);
const imageError = ref(false);
const imageLoading = ref(true);
const imageLoaded = ref(false);

let observer = null;

const handleClick = () => {
    emit('click', props.company);
};

const isValidImageUrl = (url) => {
    if (!url) return false;
    return url.startsWith('http://') || url.startsWith('https://') || url.startsWith('/');
};

const handleImageLoad = () => {
    imageLoading.value = false;
    imageLoaded.value = true;
};

const handleImageError = () => {
    imageLoading.value = false;
    imageError.value = true;
};

const getInitial = (name) => {
    return name ? name.charAt(0).toUpperCase() : '?';
};

// 懒加载实现
const loadImage = () => {
    if (imageRef.value && isValidImageUrl(props.company.logo)) {
        const dataSrc = imageRef.value.getAttribute('data-src');
        if (dataSrc) {
            imageRef.value.src = dataSrc;
        }
    }
};

onMounted(() => {
    // 使用 Intersection Observer 实现懒加载
    if ('IntersectionObserver' in window) {
        observer = new IntersectionObserver(
            (entries) => {
                entries.forEach((entry) => {
                    if (entry.isIntersecting) {
                        loadImage();
                        if (observer && cardRef.value) {
                            observer.unobserve(cardRef.value);
                        }
                    }
                });
            },
            {
                rootMargin: '50px', // 提前50px开始加载
                threshold: 0.01
            }
        );

        if (cardRef.value) {
            observer.observe(cardRef.value);
        }
    } else {
        // 不支持 IntersectionObserver 时直接加载
        loadImage();
    }
});

onUnmounted(() => {
    if (observer && cardRef.value) {
        observer.unobserve(cardRef.value);
    }
});
</script>

<style scoped>
.company-card {
    background: rgba(13, 31, 60, 0.6);
    backdrop-filter: blur(10px);
    border: 1px solid rgba(77, 208, 225, 0.3);
    border-radius: var(--radius-lg);
    padding: var(--spacing-xl);
    box-shadow: 0 0 20px rgba(77, 208, 225, 0.2);
    transition: all var(--transition-smooth);
    cursor: pointer;
    position: relative;
    display: flex;
    flex-direction: column;
    gap: var(--spacing-md);
}

/* 悬停动画效果 */
.company-card:hover {
    transform: translateY(-8px);
    box-shadow: 0 0 30px rgba(77, 208, 225, 0.4), 0 0 50px rgba(77, 208, 225, 0.2);
    background: rgba(13, 31, 60, 0.8);
    border-color: rgba(77, 208, 225, 0.6);
}

.company-card:active {
    transform: translateY(-4px);
}

.company-card:focus-visible {
    outline: 3px solid rgba(77, 208, 225, 0.6);
    outline-offset: 4px;
    transform: translateY(-4px);
    box-shadow: 0 0 30px rgba(77, 208, 225, 0.4);
}

/* 已入驻标签 */
.company-card__badge {
    position: absolute;
    top: var(--spacing-md);
    right: var(--spacing-md);
    background: var(--accent-gold);
    color: var(--neutral-dark);
    padding: var(--spacing-xs) var(--spacing-sm);
    border-radius: var(--radius-full);
    font-size: var(--font-size-xs);
    font-weight: var(--font-weight-semibold);
    line-height: 1;
    box-shadow: 0 2px 4px rgba(255, 215, 0, 0.3);
}

/* Logo区域 */
.company-card__logo {
    width: 120px;
    height: 120px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: var(--radius-md);
    overflow: hidden;
    background: rgba(26, 41, 66, 0.8);
    border: 1px solid rgba(77, 208, 225, 0.2);
    position: relative;
}

.company-card__logo img {
    width: 100%;
    height: 100%;
    object-fit: contain;
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
}

.company-card__logo img.lazy-loaded {
    opacity: 1;
}

.company-card__logo-placeholder {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: var(--font-size-4xl);
    font-weight: var(--font-weight-bold);
    background: linear-gradient(135deg, rgba(77, 208, 225, 0.2) 0%, rgba(38, 198, 218, 0.2) 100%);
    color: #4dd0e1;
    text-shadow: 0 0 10px rgba(77, 208, 225, 0.5);
}

/* 加载状态 */
.company-card__logo-loading {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(26, 41, 66, 0.8);
}

.loading-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid rgba(77, 208, 225, 0.2);
    border-top-color: #4dd0e1;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* 内容区域 */
.company-card__content {
    display: flex;
    flex-direction: column;
    gap: var(--spacing-sm);
    text-align: center;
}

.company-card__name {
    font-size: var(--font-size-xl);
    font-weight: var(--font-weight-semibold);
    color: #4dd0e1;
    margin: 0;
    line-height: var(--line-height-tight);
    text-shadow: 0 0 8px rgba(77, 208, 225, 0.4);
}

.company-card__industry {
    font-size: var(--font-size-sm);
    color: #80deea;
    font-weight: var(--font-weight-medium);
    margin: 0;
    line-height: var(--line-height-normal);
}

.company-card__description {
    font-size: var(--font-size-sm);
    color: #b0bec5;
    line-height: var(--line-height-relaxed);
    margin: 0;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* 响应式调整 */
@media (max-width: 768px) {
    .company-card {
        padding: var(--spacing-lg);
    }

    .company-card__logo {
        width: 100px;
        height: 100px;
    }

    .company-card__name {
        font-size: var(--font-size-lg);
    }
}
</style>
